package com.zy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.Notice;
import com.zy.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ZyNoticeController {
    @Autowired
    NoticeMapper noticeMapper;

    /**
     * @param pageNum  当前页
     * @param pageSize 每页记录的数量
     * @Author: 杨深俊
     * @Method: getNotices
     * @Date: 2021/4/7
     * @Time: 11:45
     * 查询所有公告
     */
    @RequestMapping("/noticeManage")
    public ModelAndView getNotices(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("NoticeManage");

        //系统公告分页
        IPage<Notice> noticeIPage = new Page<>(pageNum, pageSize);
        IPage<Notice> notices = noticeMapper.selectPage(noticeIPage, null);

        modelAndView.addObject("notices", notices);

        //返回视图
        return modelAndView;
    }

    /**
     * 通过id删除公告
     */
    @RequestMapping("/delNotice")
    public String delNotice(Integer id) {
        noticeMapper.deleteNoticeById(id);
        return "redirect:/noticeManage";
    }

    //添加公告
    @PostMapping("addNotice")
    public String addNotice(@ModelAttribute Notice notice, Integer pageNum) {
        noticeMapper.addNotice(notice);
        return "redirect:/noticeManage?pageNum=" + pageNum;
    }

    //修改公告
    @PostMapping("updateNotice")
    public String updateNotice(@ModelAttribute Notice notice, Integer pageNum) {
        noticeMapper.updateNotice(notice);
        return "redirect:/noticeManage?pageNum=" + pageNum;
    }

    @GetMapping("getNotice")
    @ResponseBody
    public Notice getNotice(Integer id) {
        Notice notice = noticeMapper.getNoticeById(id);
        System.out.println(notice);
        return notice;
    }
}